/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.bashsoft.nero.nameparser;
import java.io.IOException;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author nero
 */
public class Parser {
    
    private String sport;       
    private List<String> tournaments = new ArrayList<String>();
    private List<String> tours = new ArrayList<String>();
    private List<String> clubs = new ArrayList<String>();    
    private List<Player> players = new ArrayList<Player>();
    
    public Parser(String sport) {
        this.sport = sport;         
    }       
    
    public void parse(String startUrl, String folderName) throws IOException, ConnectException {        

        tournaments.clear();
        
        String url = startUrl;        
        String fileName = null;
        
        //HtmlPage page = HttpHelper.getHtml(url, sport);
        url = startUrl;//nextTournamentUrl(page);
        
        do {           
            if (!tournaments.contains(url))
                tournaments.add(url);
            url = parseTournamentPage(url, folderName);
        } while (url != null);    
    }
    
    private String parseTournamentPage(String url, String folderName) throws ConnectException, IOException {        
        HtmlPage page = HttpHelper.getHtml(url, sport);
        if (page == null) return null;            
        
        System.out.println("PARSE " + url);        
        
        tours.clear();
        clubs.clear();
        players.clear();               
        
        String fileName = HtmlHelper.getIdByTournamentUrl(url) + ".xml";
        fileName = folderName + java.io.File.separator + fileName;
        java.io.File file = new java.io.File(fileName);
        
        
        if (!file.exists()) {
        
            scanPlayers(page);
            scanClubs(page);
            scanTours(page);

            XmlHelper.savePlayers(players, fileName);            
        }
        
        scanSubTournaments(page, folderName);        
        
        return nextTournamentUrl(page);
    }
    
    private void parseSubTournamentPage(String url, String folderName) throws ConnectException, IOException {        
        HtmlPage page = HttpHelper.getHtml(url, sport);
        if (page == null) return;            
        
        System.out.println("PARSE " + url);    
        
        String fileName = HtmlHelper.getIdByTournamentUrl(url) + ".xml";
        fileName = folderName + java.io.File.separator + fileName;
        java.io.File file = new java.io.File(fileName);
        
        
        tours.clear();
        clubs.clear();
        players.clear();
        
        if (!file.exists()) {
        
            scanPlayers(page);
            scanClubs(page);
            scanTours(page);

            XmlHelper.savePlayers(players, fileName);            
        }
    }
    
    private void parseTourPage(String url) throws ConnectException {        
        HtmlPage page = HttpHelper.getHtml(url, sport);
        if (page == null) return;
        
        System.out.println("PARSE " + url);
        
        scanPlayers(page);
        scanClubs(page);        
    }
    
    private void parseClubPage(String url) throws ConnectException {        
        HtmlPage page = HttpHelper.getHtml(url, sport);            
        if (page == null) return;        
        
        System.out.println("PARSE " + url);
        
        scanPlayers(page);             
    }
    
    private void parsePlayerPage(String url) throws ConnectException {        
        HtmlPage page = HttpHelper.getHtml(url, sport);         
        if (page == null) return;
        
        System.out.println("PARSE " + url);
        
        Player player = new Player();
        player.name = page.getTitle();
        player.nation = page.getPlayerNation();
        if (!players.contains(player))
            this.players.add(player);
    }
    
    private void scanClubs(HtmlPage page) throws ConnectException {
        String clubUrl = null;
        do {
            clubUrl = page.getNextClubUrl();
            if (clubUrl != null) {
                if (!clubs.contains(clubUrl)) { 
                    clubs.add(clubUrl);  
                    parseClubPage(clubUrl);
                }                
            }            
        } while (clubUrl != null);
    }
    
    private void scanPlayers(HtmlPage page) throws ConnectException {
        String playerUrl = null;
        do {
            playerUrl = page.getNextPlayerUrl();
            if (playerUrl != null) {                
                    parsePlayerPage(playerUrl);
            }           
        } while (playerUrl != null);
    }
    
    private void scanTours(HtmlPage page) throws ConnectException {
        String tourUrl = null;
        do {
            tourUrl = page.getNextTourUrl();
            if (tourUrl != null) {                
                if (!tours.contains(tourUrl)) {
                    tours.add(tourUrl);
                    parseTourPage(tourUrl);
                }                
            }            
        } while (tourUrl != null);        
    }
    
     private void scanSubTournaments(HtmlPage page, String folderName) throws ConnectException, IOException {
        String tournamentUrl = null;
        do {
            tournamentUrl = page.getNextSubTournamentUrl();
            if (tournamentUrl != null) {                
                if (!tournaments.contains(tournamentUrl)) {
                    tournaments.add(tournamentUrl);
                    parseSubTournamentPage(tournamentUrl, folderName);
                }           
            }
        } while (tournamentUrl != null);
    }
    
    private String nextTournamentUrl(HtmlPage page) {        
        String tournamentUrl = null;                
        do {
            tournamentUrl = page.getNextTournamentUrl();
            if ((tournamentUrl != null) && !tournaments.contains(tournamentUrl))
                return tournamentUrl;
        } while (tournamentUrl != null);
        return null;
    }    
}